package com.homestay.service.impl;

import com.homestay.beans.Books;
import com.homestay.beans.Vip;
import com.homestay.dao.BooksDao;
import com.homestay.dao.VipDao;
import com.homestay.dao.impl.BooksDaoImpl;
import com.homestay.dao.impl.VipDaoImpl;
import com.homestay.exception.UserException;
import com.homestay.service.VipService;

import java.util.Collections;
import java.util.List;

public class VipServiceImpl implements VipService {

    VipDao vipDao = new VipDaoImpl();

    BooksDao booksDao = new BooksDaoImpl();

    @Override
    public int add(Vip vip) throws UserException {
        int size = vipDao.queryByCard(vip.getCard()).size();
        if (size > 0) {
            // 身份证号已存在
            throw new UserException("身份证号已经存在");
        }
        int count = vipDao.queryByPhone(vip.getPhone()).size();
        if (count > 0) {
            // 手机号已存在
            throw new UserException("手机号已经存在");
        }

        String sex = "1".equals(vip.getSex()) ? "男" : "女";
        String v_type = vip.getV_type();
        switch (v_type) {
            case "1":
                v_type = "普通会员";
                break;
            case "2":
                v_type = "初级会员";
                break;
            case "3":
                v_type = "中级会员";
                break;
            case "4":
                v_type = "高级会员";
                break;
            case "5":
                v_type = "至尊会员";
                break;
            default:
                v_type = "普通会员";
                break;
        }
        vip.setSex(sex);
        vip.setV_type(v_type);

        return vipDao.add(vip);
    }

    @Override
    public List<Vip> queryByPhone(String phone) {
        return vipDao.queryByPhone(phone);
    }

    @Override
    public int deleteById(String id) throws UserException {
        // 正在入住的会员不能删除
        Books books = booksDao.queryBookByVipId(id);
        if (books != null) {
            throw new UserException("会员存在入住记录，不能删除");
        }

        return vipDao.deleteById(id);
    }

    @Override
    public Vip queryById(String id) {
        return vipDao.queryById(id);
    }

    @Override
    public int update(Vip vip) {
        // 性别转换
        if("1".equals(vip.getSex())){
            vip.setSex("男");
        }else{
            vip.setSex("女");
        }
        // 会员类型转换
        switch (vip.getV_type()) {
            case "1":
                vip.setV_type("普通会员");
                break;
            case "2":
                vip.setV_type("初级会员");
                break;
            case "3":
                vip.setV_type("中级会员");
                break;
            case "4":
                vip.setV_type("高级会员");
                break;
            case "5":
                vip.setV_type("至尊会员");
                break;
            default:
                vip.setV_type("普通会员");
                break;
        }
        return vipDao.update(vip);
    }

    @Override
    public List<Vip> query(String name, String card, String phone) {
        return vipDao.query(name, card, phone);
    }

}
